﻿/*
string	PinYin()		获得中文字符串的拼音
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Lyon.Util.Helper
{
    public static class String
    {
        /// <summary>
        /// 判断字符串是否为空或者NULL
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        /// <summary>
        /// 判断字符串是否是数字
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNumber(this string s)
        {
            if (s.IsNullOrEmpty())
            {
                return false;
            }
            double d;
            return double.TryParse(s, out d);
        }

        /// <summary>
        /// 判断字符串是否为整数
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsDigit(this string s)
        {
            if (s.IsNullOrEmpty())
            {
                return false;
            }
            int i;
            return int.TryParse(s, out i);
        }

        /// <summary>
        /// 判断字符串是否满足正则pattern
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static bool IsMatch(this string s, string pattern)
        {
            if (s.IsNullOrEmpty())
            {
                return false;
            }
            return Regex.IsMatch(s, pattern);
        }

        /// <summary>
        /// 判断字符串是否是合法Email格式
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsEmail(this string s)
        {
            //@"\w+(\.\w+)*@\w+(\.\w+)*\.\w+([-.]\w+)*"
            return s.IsMatch(@"^[A-Za-z]\w+(\.\w+)*@\w+(\-\w+)*(\.\w+){1,2}$");
        }

        /// <summary>
        /// 获得匹配正则pattern的字符串
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static string Match(this string s, string pattern)
        {
            if (s.IsNullOrEmpty()) return string.Empty;
            return Regex.Match(s, pattern).Value;
        }

        /// <summary>
        /// 判断字符串是否是整型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsInt(this string s)
        {
            if (s.IsNullOrEmpty()) return false;
            try
            {
                int.Parse(s);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 将字符串转换为Int
        /// </summary>
        /// <param name="s"></param>
        /// <returns>转换成功返回整型，否则返回0</returns>
        public static int ToInt(this string s)
        {
            int i = 0;
            int.TryParse(s, out i);
            return i;
        }

        /// <summary>
        /// 将字符串转换为Float
        /// </summary>
        /// <param name="s"></param>
        /// <returns>转换成功返回float型，否则返回0.0</returns>
        public static float ToFloat(this string s)
        {
            float f = 0.0f;
            float.TryParse(s, out f);
            return f;
        }

        /// <summary>
        /// 将字符串转换为Double
        /// </summary>
        /// <param name="s"></param>
        /// <returns>转换成功返回double型，否则返回0.0</returns>
        public static double ToDouble(this string s)
        {
            double d = 0.0d;
            double.TryParse(s, out d);
            return d;
        }

        /// <summary>
        /// 将字符串转换为Camel格式
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToCamel(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToLower() + s.Substring(1);
        }

        /// <summary>
        /// 将字符串转换为Pascal格式
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToPascal(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToUpper() + s.Substring(1);
        }

        /// <summary>
        /// 将字符串转换为日期格式
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string s)
        {
            if (s.IsNullOrEmpty()) return DateTime.MinValue;
            DateTime dt = DateTime.MinValue;
            DateTime.TryParse(s, out dt);
            return dt;
        }

        /// <summary>
        /// 获得字符串的字节数组
        /// </summary>
        /// <param name="s"></param>
        /// <param name="encode">编码类型</param>
        /// <returns></returns>
        public static byte[] GetBytes(this string s, string encode = "default")
        {
            if(s.IsNullOrEmpty()) return new Byte[0];
            switch(encode)
            {
                case "UTF8":
                    {
                        return Encoding.UTF8.GetBytes(s);
                    }
                case "Unicode":
                    {
                        return Encoding.Unicode.GetBytes(s);
                    }
                case "ASCII":
                    {
                        return Encoding.ASCII.GetBytes(s);
                    }
                default:
                    {
                        return Encoding.Default.GetBytes(s);
                    }
            }
        }

        /// <summary>
        /// 将字符串倒置
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string Reverse(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            StringBuilder sb = new StringBuilder();
            for (int i = s.Length; i > 0; i--)
            {
                sb.Append(s[i - 1]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 将字符串重复n次
        /// </summary>
        /// <param name="s"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static string Repeat(this string s, int n)
        {
            if (s.IsNullOrEmpty()) return s;
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= n; i++)
            {
                sb.Append(s);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 删除字符串中的字符c
        /// </summary>
        /// <param name="s"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string RemoveAll(this string s, char c)
        {
            if (s.IsNullOrEmpty()) return s;
            return s.Replace(c.ToString(), string.Empty);
        }

        /// <summary>
        /// 删除字符串中的字符数组cArr
        /// </summary>
        /// <param name="s"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string RemoveAll(this string s, char[] cArr)
        {
            if (s.IsNullOrEmpty() || cArr.Length == 0) return s;
            foreach (char c in cArr)
            {
                s = s.RemoveAll(c);
            }
            return s;
        }

        /// <summary>
        /// 删除字符串中的字符串s
        /// </summary>
        /// <param name="s"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string RemoveAll(this string str, string s)
        {
            if (str.IsNullOrEmpty()) return str;
            return str.Replace(s, string.Empty);
        }

        /// <summary>
        /// 获得字符串左边len个字符
        /// </summary>
        /// <param name="s"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string Left(this string s, int len)
        {
            if (s.IsNullOrEmpty() || s.Length <= len) return s;
            return s.Remove(len);
        }

        /// <summary>
        /// 获得字符串右边len个字符
        /// </summary>
        /// <param name="s"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string Right(this string s, int len)
        {
            if (s.IsNullOrEmpty() || s.Length <= len) return s;
            return s.Remove(0, s.Length - len);
        }

        /// <summary>
        /// 删除字符串中的危险字符，防止SQL注入
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string RemoveBadChar(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            //危险sql注入字符：, % < > . : # & $ ^ * ` ~
            char[] badChars = new char[]{'\'',',','%','.','#','&','$','^','*','`','~','='};
            return s.ToLower().RemoveAll(badChars).Replace("<", "&lt;").Replace(">", "&gt;");
        }

        /// <summary>
        /// 删除最后一个字符
        /// </summary>
        /// <returns></returns>
        public static string RemoveLastChar(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s.Left(s.Length - 1);
        }
    }
}
